# Ryan Brutger and Alexandra Guisinger June 1, 2024
# Replicatin code for "Framing Layoffs" 
# Analysis run on macOS Catalina, MacBook Pro, 2.9 GHz Intel Core i5
# using R version 4.0.1 (2020-06-06) -- "See Things Now"

# The following code is for the media analysis

# ----------------------------------------------------------------------
# load all relevant themese and packages
# ----------------------------------------------------------------------

library("tidyverse")
library("ggplot2")
library("stargazer")
library("xtable") 
library("texreg")
library("reshape2")
library("RItools")
library("estimatr")
library("fastDummies")
library("ggpubr")
library("foreign")

#Set your working directory
# setwd() 

#Load data
data <- read_csv("Auto Media Coding - Data_Full.csv") 
data1 <- read_csv("Auto Media Coding - Data1.csv") # Top US Outlets
data2 <- read_csv("Auto Media Coding - Data2.csv") # Smaller US Outlets
data3 <- read_csv("Auto Media Coding - Data3-Canada.csv") # Top Canadian Outlets

# remove articles that were not relevant (those not discussing factory closings)
data <- subset(data[data$Relevant==1, ])
data1 <- subset(data1[data1$Relevant==1, ])
data2 <- subset(data2[data2$Relevant==1, ])
data3 <- subset(data3[data3$Relevant==1, ])

#Calculate percent of articles that assign to either the gov, GM, or market
data$assign.blame <- ifelse(data$Gov_Pol_Cause==1, 1, ifelse(data$Market_Cause==1,1, ifelse(data$GM_Cause==1, 1, 0)))
sum(data$assign.blame)/181 #0.895

#Full sample of media coding:
sum(data$Gov_Pol_Cause)/181 #0.249
sum(data$GM_Cause)/181 #0.42
sum(data$Market_Cause)/181 #0.74

# US Sample
(sum(data1$Gov_Pol_Cause) + sum(data2$Gov_Pol_Cause))/(41+45) #0.26
(sum(data1$GM_Cause) + sum(data2$GM_Cause))/(41+45)# 0.58
(sum(data1$Market_Cause) + sum(data2$Market_Cause))/(41+45) #0.72

# Canadian Sample Gov_Pol_Cause
sum(data3$Gov_Pol_Cause)/95 #0.24
sum(data3$GM_Cause)/95 #0.27
sum(data3$Market_Cause)/95 #0.76

# Generate panel (a) of Figure 1
# US Blame Attribution in Articles (Causes)
base.names <- c("Government", "General Motors", "Market Forces")
base.opin <- c(26, 58, 72)
color <- c("gray87","gray61", "gray29")

barplot(base.opin, main="", xlab="", ylim=c(0,90), 
        ylab="Percent of Articles", names.arg= base.names, col=color, space=c(.2, .2, .2))
# Exported 5.5 x 6.5 

# Generate panel (b) of Figure 1
# Canada Blame Attribution in Articles (Causes)
base.names <- c("Government", "General Motors", "Market Forces")
base.opin <- c(24, 27, 76)
color <- c("gray87","gray61", "gray29")

barplot(base.opin, main="", xlab="", ylim=c(0,90), 
        ylab="Percent of Articles", names.arg= base.names, col=color, space=c(.2, .2, .2))
# Exported 5.5 x 6.5 

## Proportion blaming trade policy
#In US
(sum(data1$Trade_Policy) + sum(data2$Trade_Policy))/(41+45) #0.20
# Blame higher tariffs
(sum(data1$Trade_Costs) + sum(data2$Trade_Costs))/(41+45) #0.093
#Blame lower tariffs
(sum(data1$Trade_No_Prot) + sum(data2$Trade_No_Prot))/(41+45) #0.081

#In Canada
sum(data3$Trade_Policy)/95 #0.25
# Blame higher tariffs
sum(data3$Trade_Costs)/95 #0.105
#Blame lower tariffs
sum(data3$Trade_No_Prot)/95 #0.074

# compare top circulation outlets versus smaller/regional outlets
#US top circulation outlets:
sum(data1$Gov_Pol_Cause)/41#0.32
sum(data1$GM_Cause)/41 #0.66
sum(data1$Market_Cause)/41 #0.76

# Generate panel (a) of Figure 7 of the appendix
# Blame attribution among top circulation US outlets
base.names <- c("Government", "General Motors", "Market Forces")
base.opin <- c(32, 66, 76)
color <- c("gray87","gray61", "gray29")

barplot(base.opin, main="", xlab="", ylim=c(0,90), 
        ylab="Percent of Articles", names.arg= base.names, col=color, space=c(.2, .2, .2))

# Smaller/regional outlets
sum(data2$Gov_Pol_Cause)/45 #0.20
sum(data2$GM_Cause)/45 #0.51
sum(data2$Market_Cause)/45 #0.69

# Generate panel (b) of Figure 7 of the appendix
# Blame attribution among smaller US outlets
base.names <- c("Government", "General Motors", "Market Forces")
base.opin <- c(20, 51, 69)
color <- c("gray87","gray61", "gray29")

barplot(base.opin, main="", xlab="", ylim=c(0,90), 
        ylab="Percent of Articles", names.arg= base.names, col=color, space=c(.2, .2, .2))

# Calculating Cohen's Kappa for intercoder reliability

#Based on 520 cells of coding used for comparison, and 510 cells of agreement between the coders, 
#we calculate the Cohen's Kappa statistic as requested by a reviewer

# Contingency table: 70 cells coded 1 in agreement, and 440 cells coded 0 in agreement
xtab <- as.table(rbind(c(70, 5), c(5, 440)))
# Descriptive statistics
diagonal.counts <- diag(xtab)
N <- sum(xtab)
row.marginal.props <- rowSums(xtab)/N
col.marginal.props <- colSums(xtab)/N
# Compute kappa (k)
Po <- sum(diagonal.counts)/N
Pe <- sum(row.marginal.props*col.marginal.props)
k <- (Po - Pe)/(1 - Pe)
k # 0.922

